package com.dimples.common.configure;

import com.dimples.common.constant.StrPool;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;

import org.apache.commons.lang3.StringUtils;

import cn.hutool.core.date.DateUtil;

/**
 * 自定义 p6spy sql输出格式
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/8/22
 */
public class P6spySqlFormatConfigure implements MessageFormattingStrategy {

    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
        return StringUtils.isNotBlank(sql) ? DateUtil.now()
                + " | 耗时 " + elapsed + " ms | SQL 语句：" + StrPool.NEWLINE + sql.replaceAll("[\\s]+", StringUtils.SPACE) +
                StrPool.SEMICOLON : StrPool.EMPTY;
    }
}
